package com.group6.exam.teacher.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.common.entity.PaperEntity;
import com.group6.exam.common.util.DBUtil;

public class PageDaoImpl implements IPageDao {

	/**
	 * 查询试卷列表信息
	 */
	public List<Map<String, Object>> maintain(PageModel pageModel,
			PaperEntity paper) {
		List<Object> paramList = new ArrayList<>();
		StringBuilder sb = new StringBuilder();
		sb.append("select * from paper");

		String paperName = paper.getPaperName();
		String subId = paper.getSubjectId();
		if (paperName != null || subId != null) {
			sb.append(" where");
		}
		if (paperName != null && !"".equals(paperName)) {
			sb.append(" paper_name like ?");
			paramList.add("%" + paperName + "%");
		}

		if (subId != null) {
			sb.append(" subject_id = ?");
			paramList.add(subId);
		}
		sb.append(" limit ?, ?");
		int pageSize = pageModel.getPageSize();
		int index = (pageModel.getCurrentPage() - 1) * pageSize;
		paramList.add(index);
		paramList.add(pageSize);
		Object[] parameters = paramList.toArray();
		return DBUtil.executeQuery(sb.toString(), parameters);
	}

	/**
	 * 查询科目名称
	 */
	public String getSubjectName(String subject_id) {
		String sql = "select subject_name from subject where subject_id=?";
		List<Map<String, Object>> list = DBUtil.executeQuery(sql, subject_id);
		return list != null && list.size() > 0 ? list.get(0)
				.get("subject_name") + "" : null;
	}

	/**
	 * 删除试卷
	 */
	public int deletePaper(String paperId) {
		String sql = "delete from paper where paper_id=?";
		return DBUtil.executeUpdate(sql, paperId);
	}

	/**
	 * 初始化试卷信息
	 */
	public Map<String, Object> initUpdate(String paperId) {
		String sql = "select * from paper where paper_id=?";
		return DBUtil.executeQuery(sql, paperId).get(0);
	}

	/**
	 * 修改试卷信息
	 */
	public int update(PaperEntity paper) {
		String sql = "update paper set paper_name=?, "
				+ "subject_id=?, total_score=?, pass_score=?, "
				+ "exam_time=?, single_number=?, single_score=?, "
				+ "multi_number=?, multi_score=?, judge_number=?, "
				+ "judge_score=?, user_id=? where paper_id=?";
		Object[] parameters = { paper.getPaperName(), paper.getSubjectId(),
				paper.getTotalScore(), paper.getPassScore(),
				paper.getExamTime(), paper.getSingleNumber(),
				paper.getSingleScore(), paper.getMultiNumber(),
				paper.getMultiScore(), paper.getJudgeNumber(),
				paper.getJudgeScore(), paper.getUserId(), paper.getPaperId()

		};
		return DBUtil.executeUpdate(sql, parameters);
	}

	/**
	 * 初始化科目名称
	 */
	public List<Map<String, Object>> initType() {
		String sql = "select * from subject";
		return DBUtil.executeQuery(sql);
	}

	/**
	 * 初始化用户名
	 */
	public List<Map<String, Object>> initUserName() {

		String sql = "select * from userinfo where role_id=2";
		return DBUtil.executeQuery(sql);
	}

	/**
	 * 添加试卷信息
	 */
	public int addPaper(PaperEntity paper) {
		String sql = "INSERT INTO paper VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";
		Object[] parameters = { paper.getPaperName(), paper.getSubjectId(),
				paper.getTotalScore(), paper.getPassScore(),
				paper.getExamTime(), paper.getSingleNumber(),
				paper.getSingleScore(), paper.getMultiNumber(),
				paper.getMultiScore(), paper.getJudgeNumber(),
				paper.getJudgeScore(), paper.getUserId() };
		return DBUtil.executeUpdate(sql, parameters);
	}

	/**
	 * 条件查询（得到科目ID）
	 */
	public String getId(String subjectName) {
		String sql = "select subject_id from subject where subject_name like ?";
		List<Map<String, Object>> list = DBUtil.executeQuery(sql, subjectName);
		return list != null && list.size() > 0 ? list.get(0).get("subject_id")
				+ "" : null;
	}

	/**
	 * 查询试卷的总条数
	 */
	public int getTotalRecords(PaperEntity paper) {
		List<Object> paramList = new ArrayList<>();
		StringBuilder sb = new StringBuilder();
		sb.append("select * from paper");

		String paperName = paper.getPaperName();
		String subId = paper.getSubjectId();
		if (paperName != null || subId != null) {
			sb.append(" where");
		}
		if (paperName != null && !"".equals(paperName)) {
			sb.append(" paper_name like ?");
			paramList.add("%" + paperName + "%");
		}

		if (subId != null) {
			sb.append(" subject_id = ?");
			paramList.add(subId);
		}
		Object[] parameters = paramList.toArray();
		// String sql = "select * from paper";
		return DBUtil.executeQuery(sb.toString(), parameters).size();
	}

	/**
	 * 得到试卷信息
	 */
	public List<Map<String, Object>> getPaperById(String paperId) {
		String sql = "select * from paper where paper_id=?";
		return DBUtil.executeQuery(sql, paperId);
	}

	/**
	 * 获取题
	 */
	public List<Map<String, Object>> getQuestionsByTypeId(String typeId,
			String number) {
		String sql = "select * from questions where questionType=? order by rand() limit ?";
		return DBUtil.executeQuery(sql, typeId, Integer.parseInt(number));
	}

}
